<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetPluginUsages;

use App\Base\Services\BaseAdminService;
use App\Http\FresnsDb\FresnsGroups\FresnsGroups;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigs;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;
use App\Plugins\FcpApi\TweetPlugin\TweetPlugin;
use App\Plugins\FcpApi\TweetPlugin\TweetPluginConfig;
use App\Plugins\FcpApi\TweetRole\TweetRole;

class FsService extends BaseAdminService
{
    public $needCommon = true;

    public function __construct()
    {
        $this->model = new FsModel();
        $this->resource = FsResource::class;
        $this->resourceDetail = FsResourceDetail::class;
    }

    public function common()
    {
        $common = parent::common();
        $common['TableName'] = FsConfig::CFG_TABLE;
        // 场景
        $common['sceneOption'] = FsConfig::SCONE_OPTION;
        $common['typeOption'] = FsConfig::TYPE_OPTION;
        // 小组
        $common['groupOption'] = FresnsGroups::buildSelectTreeData('id', 'name', ['is_enable'=>1]);

        $common['isGroupAdminOption'] = FsCOnfig::IS_GROUP_ADMIN_OPTION;
        // 语言
        $languageArr = TweetConfigService::getLanguageStatus();
        $common['language_status'] = $languageArr['language_status'];
        $common['default_language'] = $languageArr['default_language'];
        $common['multilingualoption'] = $languageArr['languagesOption'];
        // dd($common);
        // 角色Tips
        $common['roleUsersTips'] = FsConfig::ROLE_USERS_TIPS;
        $common['editerNumberTips'] = FsConfig::EDITER_NUMBER_TIPS;
        $common['isAdminTips'] = FsConfig::IS_ADMIN_TIPS;
        // 插件
        $common['plugOption'] = TweetPlugin::where('scene', 'like', '%pay%')->orWhere('scene', 'like', '%withdraw%')->get(['unikey as key', 'name as text']);
        // 角色
        $common['roleOption'] = TweetRole::buildSelectTreeData('id', 'name', []);

        //地图插件
        $common['mapOption'] = TweetPlugin::buildSelectOptionsByUnikey(TweetPluginConfig::PLUGINS_MAP);
        //获取地图服务商
        $mapArr = TweetConfigService::getItemValueArrByItemKey('maps');
        $commonMapArr = [];
        foreach ($mapArr as $v) {
            $item = [];
            $item['key'] = $v['id'];
            $item['text'] = $v['name'];
            $commonMapArr[] = $item;
        }
        $common['mapServiceOption'] = $commonMapArr;

        // 数据服务商插件
        $common['restfulPlugin'] = TweetPlugin::where('scene', 'like', '%restful%')->get(['unikey as key', 'name as text']);
        $common['expandPlugin'] = TweetPlugin::where('scene', 'like', '%expand%')->get(['unikey as key', 'name as text']);
        $common['typePlugin'] = TweetPlugin::where('scene', 'like', '%type%')->get(['unikey as key', 'name as text']);
        $common['groupPlugin'] = TweetPlugin::where('scene', 'like', '%group%')->get(['unikey as key', 'name as text']);
        $common['managePlugin'] = TweetPlugin::where('scene', 'like', '%manage%')->get(['unikey as key', 'name as text']);
        $common['memberPlugin'] = TweetPlugin::where('scene', 'like', '%member%')->get(['unikey as key', 'name as text']);
        $common['profilePlugin'] = TweetPlugin::where('scene', 'like', '%profile%')->get(['unikey as key', 'name as text']);
        // dd($common['restfulPlugin']);
        return $common;
    }

    // 获取后台设置默认语言code
    public static function getDefaultLanguage()
    {
        $languageArr = TweetConfigService::getLanguageStatus();
        $code = $languageArr['default_language'];

        return $code;
    }

    public function hookUpdateAfter($id)
    {
        $this->model->hookUpdateAfter($id);
    }
}
